package com.demo.servlet.utils;

import com.aspose.words.*;
import com.demo.servlet.entity.Staff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class CreateChart {


    public static final String DocumentBuilderAndSaveOut = "DocumentBuilderAndSaveOut.docx";
    public static final String output = "./output/";
    public static final String school_template = "school_template.docx";
    public static final long datast = new Date().getTime();
    private static final Logger LOGGER = LoggerFactory.getLogger(PieChart.class);

    public static void main(String[] args) throws  Exception {
        TimeByCalendar timeByCalendar = new TimeByCalendar();
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        long old = System.currentTimeMillis();
      String date = timeByCalendar.getTimeByCalendar();
        System.out.println("date"+date);
        //*生成柱状图*//*
        List<Staff> entity1 = new ArrayList<>();
        double[] double_one = new double[]{100};
        double[] double_two = new double[]{150};
        Staff a = new Staff("信息学院", double_one);
        Staff b = new Staff("第二个学院", double_two);
        entity1.add(a);entity1.add(b);

       /* ChartSeries series =  CreateChart(doc,builder,"标题",entity1);//生成柱状图
        SetChartSeriesDataLabel(series,entity1.size(),true);//显示数据*/

        Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
        Chart chart = shape.getChart();
        chart.getTitle().setText("标题");//标题
        ChartSeriesCollection seriesColl = chart.getSeries();
        seriesColl.clear();
        seriesColl.add("学院一", new double[]{100}, new double[]{100});
        seriesColl.add("学院二", new double[]{200}, new double[]{200});






        /********************************************************************************************/
         /*ChartDataLabelCollection dataLabelCollection = series.getDataLabels();

         for (int i =0;i<entity1.size();i++){
             // 向第一个系列的第一个点和第二个点添加数据标签
             ChartDataLabel chartDataLabel00 = dataLabelCollection.add(0);
             ChartDataLabel chartDataLabel01 = dataLabelCollection.add(1);

             // 设置属性。
             chartDataLabel00.setShowLegendKey(true);
             // By default, when you add data labels to the data points in a pie chart, leader lines are displayed for data labels that are
             // positioned far outside the end of data points. Leader lines create a visual connection between a data label and its
             // corresponding data point.
             chartDataLabel00.setShowLeaderLines(true);
             chartDataLabel00.setShowCategoryName(false);
             chartDataLabel00.setShowPercentage(false);
             chartDataLabel00.setShowSeriesName(true);
             chartDataLabel00.setShowValue(true);
             chartDataLabel00.setSeparator("/");
             chartDataLabel01.setShowValue(true);
         }*/

        /********************************************************************/
        save(doc,old);
    }


    /*柱状图*/
    private static ChartSeries CreateChart(Document doc, DocumentBuilder builder, String Title, List<Staff> entity) throws Exception {
        ChartSeries series =null;
        Shape shape = builder.insertChart(ChartType.COLUMN, 432, 252);
        Chart chart = shape.getChart();
        chart.getTitle().setText(Title);//标题
        ChartSeriesCollection seriesColl = chart.getSeries();
        seriesColl.clear();
        String[] categories = new String[]{"XXX学院"};
        for (int i =0;i<entity.size();i++){
            series=   seriesColl.add(entity.get(i).getName(), categories,entity.get(i).getDoubles());
        }
        return series;
    }

    /*柱状图中显示数据*/
    private static void SetChartSeriesDataLabel(ChartSeries series,int len,boolean showPercent){

        ChartDataLabel chartDataLabel;
        for (int i = 0; i < len; i++)
        {
            ChartDataLabelCollection dataLabelCollection = series.getDataLabels();
           // dataLabelCollection.add(0);
            chartDataLabel= dataLabelCollection.add(i);
            chartDataLabel.setShowLegendKey(true);   // 是否显示图例
            chartDataLabel.setShowLeaderLines(false);
            chartDataLabel.setShowCategoryName(true);
            chartDataLabel.setShowSeriesName(false);
            chartDataLabel.setShowValue(true);// 是否显示y轴的值
            chartDataLabel.setSeparator("\r\n");
            //chartDataLabel.set.NumberFormat.FormatCode = "0.00%";//设置数字格式
        }


    }




    private  static  void  save(Document doc,long old)throws Exception{
        String docxName = datast+DocumentBuilderAndSaveOut;
        doc.save(output+docxName);
        long now = System.currentTimeMillis();
        System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
        doc.save(output + datast + DocumentBuilderAndSaveOut);
    }
}